package com.studyweb.controller;

import com.studyweb.pojo.entity.Vocabulary;
import com.studyweb.service.VocabularyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 词语积累控制器
 */
@RestController
@RequestMapping("/api/vocabulary")
public class VocabularyController {

    @Autowired
    private VocabularyService vocabularyService;

    /**
     * 添加词语
     */
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addVocabulary(@RequestBody Vocabulary vocabulary) {
        boolean result = vocabularyService.addVocabulary(vocabulary);
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "添加成功");
            response.put("data", vocabulary);
        } else {
            response.put("code", 500);
            response.put("message", "添加失败");
        }
        
        return ResponseEntity.ok(response);
    }

    /**
     * 获取词语详情
     */
    @GetMapping("/detail/{id}")
    public ResponseEntity<Map<String, Object>> getVocabulary(@PathVariable Long id) {
        Vocabulary vocabulary = vocabularyService.getVocabularyById(id);
        
        Map<String, Object> response = new HashMap<>();
        if (vocabulary != null) {
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", vocabulary);
        } else {
            response.put("code", 404);
            response.put("message", "词语不存在");
        }
        
        return ResponseEntity.ok(response);
    }

    /**
     * 更新词语
     */
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateVocabulary(@RequestBody Vocabulary vocabulary) {
        boolean result = vocabularyService.updateVocabulary(vocabulary);
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "更新成功");
        } else {
            response.put("code", 500);
            response.put("message", "更新失败");
        }
        
        return ResponseEntity.ok(response);
    }

    /**
     * 删除词语
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Map<String, Object>> deleteVocabulary(@PathVariable Long id) {
        boolean result = vocabularyService.deleteVocabulary(id);
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "删除成功");
        } else {
            response.put("code", 500);
            response.put("message", "删除失败");
        }
        
        return ResponseEntity.ok(response);
    }

    /**
     * 批量删除词语
     */
    @DeleteMapping("/batch-delete")
    public ResponseEntity<Map<String, Object>> batchDeleteVocabulary(@RequestBody List<Long> ids) {
        boolean result = vocabularyService.batchDeleteVocabulary(ids);
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "批量删除成功");
        } else {
            response.put("code", 500);
            response.put("message", "批量删除失败");
        }
        
        return ResponseEntity.ok(response);
    }

    /**
     * 获取词汇题目列表（用于练习模块）
     */
    @GetMapping("/questions")
    public ResponseEntity<Map<String, Object>> getQuestions(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        
        Map<String, Object> result = vocabularyService.getVocabularyList(page, pageSize, null);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取词汇题目列表成功");
        response.put("data", result);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 分页查询词语列表
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getVocabularyList(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword) {
        
        Map<String, Object> result = vocabularyService.getVocabularyList(pageNum, pageSize, keyword);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取成功");
        response.put("data", result);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 获取练习词语
     */
    @GetMapping("/practice")
    public ResponseEntity<Map<String, Object>> getPracticeVocabularies(
            @RequestParam(defaultValue = "0") int count) {
        
        List<Vocabulary> vocabularies = vocabularyService.getPracticeVocabularies(count);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取成功");
        response.put("data", vocabularies);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 获取所有词语列表（用于积累模块）
     */
    @GetMapping("/all")
    public ResponseEntity<Map<String, Object>> getAllVocabularies() {
        List<Vocabulary> vocabularies = vocabularyService.getAllVocabularies();
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取成功");
        response.put("data", vocabularies);
        
        return ResponseEntity.ok(response);
    }

    /**
     * 更新词语掌握程度
     */
    @PutMapping("/mastery/{id}")
    public ResponseEntity<Map<String, Object>> updateMasteryLevel(
            @PathVariable Long id,
            @RequestParam Integer masteryLevel,
            @RequestParam Integer reviewCount) {
        
        boolean result = vocabularyService.updateMasteryLevel(id, masteryLevel, reviewCount);
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "更新成功");
        } else {
            response.put("code", 500);
            response.put("message", "更新失败");
        }
        
        return ResponseEntity.ok(response);
    }
}